//
//  GSMainViewController.m
//  GameSky
//
//  Created by lanou on 16/1/9.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSMainViewController.h"

@interface GSMainViewController ()

@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation GSMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self addNavBar];

}

- (void)addNavBar {
    // 隐藏导航栏;
    self.navigationController.navigationBarHidden = YES;
    self.navBar = [[GSMainNavigationBar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    [self.view addSubview:self.navBar];
    [self.navBar.leftButton addTarget:self action:@selector(showMenu:) forControlEvents:(UIControlEventTouchUpInside)];
}

// 菜单按钮点击方法;
- (void)showMenu:(UIBarButtonItem *)barButton {
//    CGRect newFrame = self.tabBarController.view.frame;
//    // 如果x为0视图移到右侧,否则视图移到左侧;
//    if (newFrame.origin.x == 0) {
//        newFrame.origin.x = kScreenWidth * 0.7;
//
//    } else {
//        newFrame.origin.x = 0;
//
//    }
//    // 添加动画效果;
//    [UIView animateWithDuration:0.5 animations:^{
//        self.tabBarController.view.frame = newFrame;
//    }];
//    - (void)openDrawer:(id)sender
//    {
//        [self.drawer open];
//    }
    
//    self.isSelected = !self.isSelected;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(showListMenu:)]) {
        
        
        
        [self.delegate showListMenu:self.isSelected];
        
        
        
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}


// 添加手势;
- (void)addGestures {
    // 1.添加边缘扫手势;
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.view addGestureRecognizer:pan];
    // 2.添加轻点手势;
    self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.view addGestureRecognizer:self.tap];
}
// 平移手势方法;
- (void)panAction:(UIPanGestureRecognizer *)pan {
    // 1.获得手势位置;
    CGFloat x = [pan locationInView:self.navigationController.view.superview].x;
    // 2.根据手势位置改变视图位置;
    CGRect newFrame = self.tabBarController.view.frame;
    if (x > kScreenWidth * 0.7) {
        x = kScreenWidth * 0.7;
    }
    newFrame.origin.x = x;
//    self.navigationController.view.frame = newFrame;
    // 3.判断手势结束时视图位置,如果大于屛宽一半滑到右侧,否则回到左侧;
    if (pan.state == UIGestureRecognizerStateEnded) {
        if (x > kScreenWidth / 2) {
            newFrame.origin.x = kScreenWidth * 0.7;
            [self.view addGestureRecognizer:self.tap];
        } else {
            newFrame.origin.x = 0;
            [self.view removeGestureRecognizer:self.tap];
        }
    }
    // 4.添加动画效果;
    [UIView animateWithDuration:0.5 animations:^{
        self.tabBarController.view.frame = newFrame;
    }];
}
// 轻拍手势方法;
- (void)tapAction:(UITapGestureRecognizer *)tap {
    CGRect newFrame = self.tabBarController.view.frame;
    newFrame.origin.x = 0;
    [UIView animateWithDuration:0.5 animations:^{
        self.tabBarController.view.frame = newFrame;
    }];
    // 视图位置还原时移除手势;
    [self.view removeGestureRecognizer:tap];
}




@end
